// pages/organization/modify.js
var app = getApp();
var util = require("../../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    org_id: 0,
    org: [],
    org_avatar_preview: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var org_id = options.org_id;
    console.log(org_id);
    wx.setStorageSync('tempFilePaths', '');
    wx.setStorageSync('org_id', org_id);
    var own_orgs = wx.getStorageSync("own_orgs");
    var org = own_orgs[org_id];
    console.log(org);
    that.setData({
      org: org,
      org_id: org_id,
      org_avatar_preview: org.avatar_thumbnail_url
    });
    wx.setNavigationBarTitle({
      title: "管理组织"
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  add_org_avatar: function () {
    var that = this;
    console.log('Now Add Org Avatar..');
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths);
        //org_avatar_preview = tempFilePaths;
        wx.setStorageSync('tempFilePaths', tempFilePaths)
        that.setData({
          org_avatar_preview: tempFilePaths
        })
        return;
      }
    })
  },
  edit_request: function (e) {
    var that = this;
    var value = e.detail.value;

    wx.showModal({
      title: '编辑组织',
      content: '确认提交？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          var org_name = encodeURIComponent(value.org_name);
          var org_intro = encodeURIComponent(value.org_intro);
          var tempFilePaths = wx.getStorageSync('tempFilePaths');
          var yuyue_session = wx.getStorageSync('yuyue_session');
          var token = wx.getStorageSync('token');
          var org_id = wx.getStorageSync('org_id');
          console.log(tempFilePaths[0]);
          console.log(org_name);
          console.log(org_intro);
          console.log(org_id);
          //return false;
          if (tempFilePaths == '' || tempFilePaths == undefined) {//没有修改图片
            wx.request({
              url: app.globalData.default_url,
              data: {
                action: "api.v1.organization.edit",
                org_name: (org_name),
                org_intro: (org_intro),
                yuyue_session: yuyue_session,
                token: token,
                org_id: org_id,
              },
              success: function (res) {//请求成功
                console.log(res.data);
                if (res.data.op == 'org_edit') {//修改成功
                  console.log("org_edited");
                  wx.showToast({
                    title: '修改成功',
                  })
                  wx.navigateBack({ delta: 2 });
                } else {//修改失败
                  wx.showModal({
                    title: '修改失败',
                    content: res.data.reason,
                    showCancel: true,
                    cancelText: '取消',
                    cancelColor: '',
                    confirmText: '重试',
                    confirmColor: '',
                    success: function (res) {
                      if (res.confirm)
                        that.edit_request(e);

                    },
                    fail: function (res) { },
                    complete: function (res) { },
                  })
                }
              }, fail: function (res) {//请求失败
                neterror_Modal(function () {
                  that.edit_request(e);
                  return;
                })
              }
            });
            return;//返回
          }
          var uploadTask = wx.uploadFile({//修改图片
            url: app.globalData.default_url + '/?action=api.v1.organization.edit',
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {
              'org_name': (org_name),
              'org_intro': (org_intro),
              'yuyue_session': yuyue_session,
              'token': token,
              'org_id': org_id,
            },
            success: function (res) {//请求成功
              var data = res.data
              data = JSON.parse(data);
              console.log(data);
              if (data.op == 'org_edit') {//修改成功
                console.log("org_edited");
                wx.showToast({
                  title: '修改成功',
                })
                wx.navigateBack({ delta: 2 });
              } else {//修改失败
                wx.showModal({
                  title: '修改失败',
                  content: res.data.reason,
                  showCancel: true,
                  cancelText: '取消',
                  confirmText: '重试',
                  success: function (res) {
                    if (res.confirm)
                      that.edit_request(e);

                  },
                  fail: function (res) { },
                  complete: function (res) { },
                })
              }
            },
            fail: function (data) {
              neterror_Modal(function () {
                that.edit_request(e);
                return;
              });
            },
            complete: function (data) {
              console.log("org_edit complete");
              console.log(data);
            }

          })

        } else if (res.cancel) {
          console.log('用户点击取消')
        }

      }
    })
  },
})